<?php

class PublicacionesController extends Zend_Controller_Action
{
    function init()
    {
        $this->initView();
        Zend_Loader::loadClass('Publicacion');
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
    }

    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
    }

    function indexAction()
    {
        $this->view->title = "Listado de Publicaciones";

        $publicacion = new Publicacion();
        $this->view->publicaciones = $publicacion->fetchAll();
        $this->render();
    }

    function agregarAction()
    {
        $this->view->title = "Agregar Publicacion";

        if ($this->_request->isPost()) {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();

            $autor = $filter->filter($this->_request->getPost('autor'));
            $autor = trim($autor);
            $titulo = trim($filter->filter($this->_request->getPost('titulo')));

            if ($autor != '' && $titulo != '') {
                $data = array(
                    'autor' => $autor,
                    'titulo'  => $titulo,
                );
                $publicaciones = new Publicacion();
                $publicaciones->insert($data);

                $this->_redirect('/publicaciones/');
                return;
            }
        }

        // set up an "empty" publicacion
        $this->view->publicaciones = new stdClass();
        $this->view->publicaciones->id = null;
        $this->view->publicaciones->autor = '';
        $this->view->publicaciones->titulo = '';

        // additional view fields required by form
        $this->view->action = 'agregar';
        $this->view->buttonText = 'Agregar';

        $this->render();
    }

    function editAction()
    {
        $this->view->title = "Editar Publicacion";
        $publicaciones = new Publicacion();

        if ($this->_request->isPost()) {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();

            $id = (int)$this->_request->getPost('id');
            $autor = $filter->filter($this->_request->getPost('autor'));
            $autor = trim($autor);
            $titulo = trim($filter->filter($this->_request->getPost('titulo')));

            if ($id !== false) {
                if ($autor != '' && $titulo != '') {
                    $data = array(
                        'autor' => $autor,
                        'titulo'  => $titulo,
                    );
                    $where = 'id = ' . $id;
                    $publicaciones->update($data, $where);

                    $this->_redirect('/publicaciones/');
                    return;
                } else {
                    $this->view->publicaciones = $publicaciones->fetchRow('id='.$id);
                }
            }
        } else {
            // album id should be $params['id']
            $id = (int)$this->_request->getParam('id', 0);
            if ($id > 0) {
                $this->view->publicaciones = $publicaciones->fetchRow('id='.$id);
            }
        }

        // additional view fields required by form
        $this->view->action = 'edit';
        $this->view->buttonText = 'Update';

        $this->render();
    }

    function deleteAction()
    {
        $this->view->title = "Eliminar Publicacion";

        $publicaciones = new Publicacion();
        if ($this->_request->isPost()) {
            Zend_Loader::loadClass('Zend_Filter_Alpha');
            $filter = new Zend_Filter_Alpha();

            $id = (int)$this->_request->getPost('id');
            $del = $filter->filter($this->_request->getPost('del'));

            if ($del == 'Yes' && $id > 0) {
                $where = 'id = ' . $id;
                $rows_affected = $publicaciones->delete($where);
            }
        } else {
            $id = (int)$this->_request->getParam('id');
            if ($id > 0) {
                // only render if we have an id and can find the album.
                $this->view->publicaciones = $publicaciones->fetchRow('id='.$id);

                if ($this->view->publicaciones->id > 0) {
                    $this->render();
                    return;
                }
            }
        }

        // redirect back to the album list unless we have rendered the view
        $this->_redirect('/publicaciones/');
    }
}